package me.stone.stanimeclient.page.setting

import androidx.annotation.StringRes
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.launch
import me.stone.stanimeclient.core.common.utils.WhileViewSubscribed
import me.stone.stanimeclient.core.data.domain.setting.SetDarkThemeModeCase
import me.stone.stanimeclient.core.model.DarkThemeMode
import me.stone.stanimeclient.core.ui.R
import me.stone.stanimeclient.core.ui.binding.item.ObserverFlowSetting
import me.stone.stanimeclient.ui.DarkThemeModeDelegate
import javax.inject.Inject

@HiltViewModel
class SettingViewModel @Inject constructor(
    private val setDarkThemeModeCase: SetDarkThemeModeCase,
    private val themeModeDelegate: DarkThemeModeDelegate
) : ViewModel(), DarkThemeModeDelegate by themeModeDelegate {

    private val _popDarkThemeDialog = MutableSharedFlow<Unit>()
    val popDarkThemeSelectorDialogSign = _popDarkThemeDialog
        .shareIn(viewModelScope, WhileViewSubscribed)

    val darkThemeSetting = ObserverFlowSetting.newSettingWithSwitch(
        coroutineScope = viewModelScope,
        iconResId = R.drawable.ic_common_dark_theme_mode,
        titleResId = R.string.dark_theme_mode,
        descResId = currentDarkThemeMode.settingDescId,
        checked = currentDarkThemeMode == DarkThemeMode.Dark,
        onSettingItemClick = { popDarkThemeSelectorDialog() },
        onCheckedChanged = { updateDarkThemeMode(
            if (it) DarkThemeMode.Dark
            else DarkThemeMode.Light
        ) }
    )

    private val DarkThemeMode.settingDescId: Int
        @StringRes get() = when (this) {
            DarkThemeMode.Light -> R.string.close
            DarkThemeMode.Dark -> R.string.open
            DarkThemeMode.System -> R.string.flow_system
        }

    private fun popDarkThemeSelectorDialog() {
        viewModelScope.launch { _popDarkThemeDialog.emit(Unit) }
    }

    fun updateDarkThemeMode(mode: DarkThemeMode) {
        viewModelScope.launch {
            if (mode == currentDarkThemeMode) { return@launch }
            setDarkThemeModeCase(mode)
        }
    }

    fun updateDarkThemeSettingUI(mode: DarkThemeMode) {
        darkThemeSetting.run {
            descriptionResId.update(mode.settingDescId)
            checked.update(mode == DarkThemeMode.Dark)
        }
    }


    private val _showLibs = MutableSharedFlow<Unit>()
    val showLib = _showLibs
        .shareIn(viewModelScope, SharingStarted.Eagerly)

    val infoLibraries = ObserverFlowSetting.newTitleDescItem(
        coroutineScope = viewModelScope,
        iconResId = R.drawable.ic_common_info,
        titleResId = R.string.about,
        descResId = R.string.libraries_version
    ) {
        viewModelScope.launch { _showLibs.emit(Unit) }
    }


    fun onAttachedToBinding() {
        updateDarkThemeSettingUI(currentDarkThemeMode)
    }

}